package util;

import org.junit.Test;

import java.sql.*;

public class DBUtil {
    @Test
    public void excute(){
        try{
            //注册mysql驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取数据库连接,第一个参数是连接字符串，第二个是用户名，第三个是密码
            Connection connection= DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=Asia/Shanghai","root",""
            );
            Statement stat=connection.createStatement();//创建一个传输sql语句的对象
            /*//删除一条course表记录
            String sql1="delete  from course where id=1";
            stat.execute(sql1);
            //新增一条course表记录
            String sql="INSERT into course VALUES(1,'PYthon','201')";
            stat.execute(sql);
            //更新一条course表记录

            String sql2="update  course set name='JavaScript' where id=2";
            stat.execute(sql2);*/
            ResultSet rs=stat.executeQuery("SELECT b.*,score FROM score a,student b,course c where a.student_id=b.Id and a.course_id=c.Id");//执行sql并获取sql查询结果
            //显示rs对象中的数据;rs.next()遍历指针
            while (rs.next()){
                System.out.print(rs.getInt("Id")+" ");
                System.out.print(rs.getString("name")+" ");
                System.out.print(rs.getString("clazz")+" ");
                System.out.println(rs.getDouble("score")+" ");
            }
            //关闭连接释放资源
            rs.close();;
            stat.close();
            connection.close();

        }
        catch (ClassNotFoundException | SQLException e){
            e.printStackTrace();
        }


    }
}
